from django.db import models


class HostGroup(models.Model):
    """主机组"""
    name = models.CharField(max_length=100, unique=True, verbose_name='组名')
    description = models.TextField(blank=True, verbose_name='描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'host_group'
        verbose_name = '主机组'
        verbose_name_plural = '主机组'
        ordering = ['-created_at']

    def __str__(self):
        return self.name


class Project(models.Model):
    """项目"""
    name = models.CharField(max_length=100, unique=True, verbose_name='项目名')
    code = models.CharField(max_length=50, unique=True, verbose_name='项目代码')
    description = models.TextField(blank=True, verbose_name='描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'project'
        verbose_name = '项目'
        verbose_name_plural = '项目'
        ordering = ['-created_at']

    def __str__(self):
        return self.name


class Environment(models.Model):
    """环境"""
    ENV_CHOICES = [
        ('dev', '开发环境'),
        ('test', '测试环境'),
        ('uat', 'UAT环境'),
        ('prod', '生产环境'),
    ]
    
    name = models.CharField(max_length=100, verbose_name='环境名')
    env_type = models.CharField(max_length=20, choices=ENV_CHOICES, verbose_name='环境类型')
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='environments', verbose_name='所属项目')
    description = models.TextField(blank=True, verbose_name='描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'environment'
        verbose_name = '环境'
        verbose_name_plural = '环境'
        ordering = ['-created_at']
        unique_together = ['project', 'env_type']

    def __str__(self):
        return f"{self.project.name} - {self.name}"


class Host(models.Model):
    """主机"""
    STATUS_CHOICES = [
        ('online', '在线'),
        ('offline', '离线'),
        ('maintenance', '维护中'),
    ]
    
    SERVER_TYPE_CHOICES = [
        ('database', '数据库服务器'),
        ('web', 'Web服务器'),
        ('ansible', 'Ansible服务器'),
        ('cache', '缓存服务器'),
        ('mq', '消息队列服务器'),
        ('storage', '存储服务器'),
        ('monitor', '监控服务器'),
        ('other', '其他'),
    ]
    
    hostname = models.CharField(max_length=100, unique=True, verbose_name='主机名')
    ip = models.GenericIPAddressField(verbose_name='IP地址')
    port = models.IntegerField(default=22, verbose_name='SSH端口')
    username = models.CharField(max_length=50, default='root', verbose_name='SSH用户名')
    password = models.CharField(max_length=200, blank=True, verbose_name='SSH密码')
    ssh_key = models.TextField(blank=True, verbose_name='SSH私钥')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='online', verbose_name='状态')
    server_type = models.CharField(max_length=20, choices=SERVER_TYPE_CHOICES, default='other', verbose_name='服务器类型')
    
    host_group = models.ForeignKey(HostGroup, on_delete=models.SET_NULL, null=True, blank=True, 
                                   related_name='hosts', verbose_name='主机组')
    environment = models.ForeignKey(Environment, on_delete=models.SET_NULL, null=True, blank=True,
                                   related_name='hosts', verbose_name='环境')
    
    cpu_cores = models.IntegerField(null=True, blank=True, verbose_name='CPU核心数')
    memory_gb = models.FloatField(null=True, blank=True, verbose_name='内存(GB)')
    disk_gb = models.FloatField(null=True, blank=True, verbose_name='磁盘(GB)')
    os_version = models.CharField(max_length=100, blank=True, verbose_name='操作系统')
    
    description = models.TextField(blank=True, verbose_name='描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'host'
        verbose_name = '主机'
        verbose_name_plural = '主机'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.hostname} ({self.ip})"


class UserGroup(models.Model):
    """用户组"""
    name = models.CharField(max_length=100, unique=True, verbose_name='组名')
    description = models.TextField(blank=True, verbose_name='描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'user_group'
        verbose_name = '用户组'
        verbose_name_plural = '用户组'
        ordering = ['-created_at']

    def __str__(self):
        return self.name


class User(models.Model):
    """用户"""
    ROLE_CHOICES = [
        ('admin', '管理员'),
        ('developer', '开发人员'),
        ('operator', '运维人员'),
        ('viewer', '查看者'),
    ]
    
    username = models.CharField(max_length=50, unique=True, verbose_name='用户名')
    email = models.EmailField(unique=True, verbose_name='邮箱')
    real_name = models.CharField(max_length=50, verbose_name='真实姓名')
    role = models.CharField(max_length=20, choices=ROLE_CHOICES, default='viewer', verbose_name='角色')
    user_group = models.ForeignKey(UserGroup, on_delete=models.SET_NULL, null=True, blank=True,
                                   related_name='users', verbose_name='用户组')
    is_active = models.BooleanField(default=True, verbose_name='是否激活')
    description = models.TextField(blank=True, verbose_name='描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'user'
        verbose_name = '用户'
        verbose_name_plural = '用户'
        ordering = ['-created_at']

    def __str__(self):
        return self.username


class Recipient(models.Model):
    """邮件收件人"""
    name = models.CharField(max_length=100, verbose_name='姓名')
    email = models.EmailField(unique=True, verbose_name='邮箱地址')
    phone = models.CharField(max_length=20, blank=True, verbose_name='手机号')
    department = models.CharField(max_length=100, blank=True, verbose_name='部门')
    position = models.CharField(max_length=100, blank=True, verbose_name='职位')
    is_active = models.BooleanField(default=True, verbose_name='是否启用')
    description = models.TextField(blank=True, verbose_name='备注')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'recipient'
        verbose_name = '邮件收件人'
        verbose_name_plural = '邮件收件人'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.name} ({self.email})"


# 导入用户资料模型
from .user_profile_models import UserProfile
